package com.finance.system.user.service.impl;

import com.finance.system.support.BaseServiceImpl;
import com.finance.system.support.bean.ResultBean;
import com.finance.system.support.bean.ResultConstant;
import com.finance.system.support.utils.DomainTransHelper;
import com.finance.system.user.entity.GroupEntity;
import com.finance.system.user.entity.GroupInbound;
import com.finance.system.user.entity.UserGroupRelationEntity;
import com.finance.system.user.enums.GroupType;
import com.finance.system.user.mapper.GroupMapper;
import com.finance.system.user.service.IGroupService;
import com.finance.system.user.service.IUserGroupRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;


/**
 * <p>
 * 组的信息表，成员的集合 服务实现类
 * </p>
 *
 * @author whdong
 * @since 2019-12-24
 */
@Service
public class GroupServiceImpl extends BaseServiceImpl<GroupMapper, GroupEntity> implements IGroupService {

    @Autowired
    private IUserGroupRelationService userGroupRelationService;

    @Override
    public ResultBean<List<GroupEntity>> getGroupByUserId(String userId) {
        if(StringUtils.isEmpty(userId)){
            return ResultBean.create(ResultConstant.SUCCESS_CODE,ResultConstant.SUCCESS_MESSAGE, Collections.emptyList());
        }
        List<UserGroupRelationEntity> relations = userGroupRelationService.getByUserId(userId,null);
        List<Long> groupIds =
                relations.stream().map(UserGroupRelationEntity::getGroupId).collect(Collectors.toList());
        Collection<GroupEntity> groups = this.listByIds(groupIds);
        return ResultBean.create(ResultConstant.SUCCESS_CODE,ResultConstant.SUCCESS_MESSAGE,
                new ArrayList<>(groups));

    }

    @Override
    public ResultBean<GroupEntity> updateGroup(GroupInbound group) {
        if(group.getId() == null){
            return ResultBean.create(ResultConstant.SUCCESS_CODE,ResultConstant.SUCCESS_MESSAGE,null);
        }
        GroupEntity groupEntity = DomainTransHelper.transFromTo(group, GroupEntity.class);
        this.updateById(groupEntity);
        return ResultBean.create(ResultConstant.SUCCESS_CODE,ResultConstant.SUCCESS_MESSAGE,groupEntity);
    }

    @Override
    public ResultBean<GroupEntity> addOne(GroupInbound group) {
        if(StringUtils.isEmpty(group.getCreater())){
            return ResultBean.create(ResultConstant.ERROR_CODE,"创建者不能为空",null);
        }
        //创建新的账本
        GroupEntity newGroup = addNewGroup(group);
        //建立用户账本关系
        UserGroupRelationEntity relation = addNewGroupRelation(newGroup);
        return ResultBean.create(ResultConstant.SUCCESS_CODE,ResultConstant.ADD_SUCCESS_MESSAGE,
                newGroup);
    }

    private UserGroupRelationEntity addNewGroupRelation(GroupEntity newGroup) {
        UserGroupRelationEntity entity = new UserGroupRelationEntity();
        entity.setGroupId(newGroup.getId());
        entity.setUserId(newGroup.getCreater());
        userGroupRelationService.save(entity);
        return entity;
    }

    private GroupEntity addNewGroup(GroupInbound group) {
        GroupEntity groupEntity = DomainTransHelper.transFromTo(group, GroupEntity.class);
        groupEntity.setGroupType(GroupType.NORMAL.name());
        this.save(groupEntity);
        return groupEntity;
    }
}
